﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CentralOffice.Archive.Data;
using CentralOffice.Archive.BusinessActions;

using CategoryTable = CentralOffice.Archive.Data.CategoryDataSet.CategoryDataTable;
using CategoryRow = CentralOffice.Archive.Data.CategoryDataSet.CategoryRow;

namespace CentralOffice.WebClient.Archives
{
    public partial class CategoryManagement : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            GridViewRow row = ((LinkButton)sender).Parent.Parent as GridViewRow;
            string id = this.gv.DataKeys[row.RowIndex].Value.ToString();
            SetFields(id);

            updPnl.Update();
            //txtWellCode.Enabled = false;
            mdlPopup.Show();
        }

        private void SetFields(string id)
        {
            CategoryRow row = ArchiveCategory.GetById(new Guid(id));

            txtName.Text = row.Name;
            txtId.Value = row.Id.ToString();
            txtCode.Text = row.QueryNumber;
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            //ClearFields();
            //txtWellCode.Enabled = true;
            mdlPopup.Show();

        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (txtName.Text.Trim().Length == 0)
                return;
            string id = txtId.Value;
            CategoryRow row = GetFields(id);

            if (row != null)
            {
                ArchiveCategory.Update(row.Table.DataSet);

                upCategory.Update();
                updPnl.Update();
                //this.ViewState.Remove("WellId");
                txtId.Value = "";
                ClearFields();
            }
        }

        private void ClearFields()
        {
            txtId.Value = "";
            txtName.Text = "";
            txtCode.Text = "";
        }

        private CategoryRow GetFields(string id)
        {
            CategoryRow row = null;
            if (string.IsNullOrEmpty(id))
            {

                //row = ArchiveCategory.GetAll();

                row = (new CategoryDataSet()).Category.NewCategoryRow();
                SiteUtil.PopulateCommonField(row, Guid.NewGuid());
            }
            else
            {
                row = ArchiveCategory.GetById(new Guid(id));
            }

            row.Name = txtName.Text;
            row.QueryNumber = Husb.Util.CommonUtil.GetChineseSpell(txtName.Text);

            row.ModifiedTime = DateTime.Now;
            row.LastModifiedBy = ApplicationContext.ActualUserId;

            if (string.IsNullOrEmpty(id))
            {
                row.Table.Rows.Add(row);
            }

            return row;

        }
    }
}
